package com.alinesno.cloud.initializr.starter.gateway.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.constants.FieldConstants;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.codegen.entity.GenTable;
import com.alinesno.cloud.initializr.starter.codegen.service.IGenTableService;
import com.alinesno.cloud.initializr.starter.dto.CheckDbConnectResult;
import com.alinesno.cloud.initializr.starter.entity.InitializrDbListEntity;
import com.alinesno.cloud.initializr.starter.gateway.aop.DataFilter;
import com.alinesno.cloud.initializr.starter.service.IInitializrDbListService;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;
import com.alinesno.cloud.rest.gateway.adapter.security.CurrentAccountJwt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 数据库管理Controller
 *
 * @author alinesno
 * @date 2021-05-10
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("/com/alinesno/cloud/initializr/db/database")
public class InitializrDbListRest extends BaseRestAdapter<InitializrDbListEntity, IInitializrDbListService> {

    //日志记录
    private static final Logger log = LoggerFactory.getLogger(InitializrDbListRest.class);
    @Autowired
    private IInitializrDbListService initializrDbListService;
    
    @Autowired
    private IGenTableService genTableService;

    @TranslateCode
    @ResponseBody
    @DataFilter
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
        log.debug("page = {}", ToStringBuilder.reflectionToString(page));
        return this.toPage(model, getFeign(), page);
    }

//    @Override
//    public ResponseBean save(Model model, HttpServletRequest request, InitializrDbListEntity entity) {
//        try {
//            return super.save(model, request, entity);
//        } catch (Exception e) {
//            return ResponseGenerator.fail("数据库连接失败");
//        }
//    }

    /**
     * 同步数据库
     */
    @GetMapping("/syncDataBase/{id}")
    public AjaxResult syncDataBase(@PathVariable String id) {
        initializrDbListService.syncDataBase(id);
        return AjaxResult.success();
    }

    @PostMapping("/checkDB")
    @ResponseBody
    public AjaxResult checkDBConnect(@RequestBody InitializrDbListEntity dbListEntity) {
        CheckDbConnectResult result = initializrDbListService.checkDbConnect(dbListEntity);
        if (result.isAccepted()) {
            return AjaxResult.success("操作成功", result);
        } else {
            return AjaxResult.error("数据库检验失败", result);
        }
    }

    @PostMapping("/saveConfig")
    @ResponseBody
    public AjaxResult saveConfig(GenTable genTable) {
        genTableService.updateMeta(genTable);
        return AjaxResult.success();
    }

    @GetMapping("databaseList")
    protected AjaxResult databaseList() {
    	QueryWrapper<InitializrDbListEntity> wrapper = new QueryWrapper<>() ; 
    
    	wrapper.eq(FieldConstants.OPERATOR_ID, CurrentAccountJwt.getUserId()) ;
    	
        List<InitializrDbListEntity> list = this.getFeign().list(wrapper) ; 
        return toAjax(list);
    }

    @Override
    public IInitializrDbListService getFeign() {
        return this.initializrDbListService;
    }
}
